<script setup lang="ts">
import { RouterView } from 'vue-router'
import HeaderMain from './components/HeaderMain.vue'
import { onMounted, ref, watch } from 'vue'
import router from './router'
import MenuSider from './components/MenuSider.vue'
import TabList from './components/TabList.vue'
import { useCommonStore } from './stores/common'

const currentRoute = ref('')
const breadcrumbsList = ref<commonTypes.menuItem[]>([])
onMounted(() => {
  breadcrumbsList.value = JSON.parse(sessionStorage.getItem('breadcrumbsList') as string) || []
})
watch(
  () => router.currentRoute.value,
  (toPath) => {
    if (!sessionStorage.getItem('xunfei_token')) {
      sessionStorage.clear()
      useCommonStore().setBreadcrumbsList([])
      router.push('/login')
    }
    currentRoute.value = toPath.path || ''
  },
  { immediate: true, deep: true }
)
watch(
  () => useCommonStore().breadcrumbsList,
  (info) => {
    breadcrumbsList.value = info || []
  },
  { immediate: true, deep: true }
)
</script>

<template>
  <div class="common-layout">
    <el-container v-if="currentRoute !== '/login'">
      <el-header class="p0"><HeaderMain /></el-header>
      <el-container>
        <!-- <el-aside class="aside" width="300px">
          <MenuSider />
        </el-aside> -->
        <el-main class="p0 ctn bg-white">
          <div class="itemMain">
            <div class="tabBox" v-if="breadcrumbsList && breadcrumbsList.length > 0">
              <TabList />
            </div>
            <div class="mainBox" ref="mainBox">
              <router-view v-slot="{ Component }">
                <Transition name="fade" mode="out-in">
                  <keep-alive>
                    <component :is="Component" :key="$route.name" v-if="$route.meta.keepAlive" />
                  </keep-alive>
                </Transition>
                <Transition name="fade" mode="out-in">
                  <component :is="Component" v-if="!$route.meta.keepAlive" />
                </Transition>
              </router-view>
            </div>
          </div>
        </el-main>
      </el-container>
    </el-container>
    <RouterView v-else />
  </div>
</template>

<style scoped lang="less">
.common-layout {
  height: 100vh;
  .el-container {
    // height: 100%;
  }
}
.p0 {
  padding: 0;

  overflow: hidden;
}
.aside {
  background: #fff;
  height: 100vh;
  border-right: 1px solid #d3d3d3;
}
.ctn {
  box-sizing: border-box;
  padding: 20px;
  .itemMain {
    height: 100%;
    width: 100%;
  }
  .mainBox {
    position: relative;
    width: 100%;
    overflow: hidden;
    height: 100%;
    .tabBox {
      padding: 10px;
      padding-bottom: 0px;
      // background: #fff;
      // position: fixed;
    }
  }
}
.fade-enter-active,
.fade-leave-active {
  transition: all 0.2s ease;
}

.fade-enter-from,
.fade-leave-active {
  opacity: 0;
}
</style>
